import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../views/frontend/landing-page/index.vue')
    },
    {
      path: '/code',
      name: 'code',
      component: () => import('../views/frontend/code-showacse/index.vue')
    },
    {
      path: '/market',
      name: 'market',
      component: () => import('../views/frontend/market/index.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/frontend/login/index.vue')
    },
    {
      path: '/unauthorized',
      name: 'unauthorized',
      component: () => import('../components/Unauthorized.vue')
    },
    {
      path: '/error',
      name: 'error',
      component: () => import('../components/ErrorPage.vue'),
      props: route => ({
        errorCode: route.query.code || 500,
        errorMessage: route.query.message || '服务器内部错误'
      })
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'notFound',
      component: () => import('../components/NotFound.vue')
    }
  ],
})

// 全局错误处理
router.onError((error) => {
  console.error('Router error:', error)
  router.push({
    name: 'error',
    query: {
      code: 500,
      message: error.message || '发生了一个错误'
    }
  })
})

export default router
